﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class CMS_Control_ReViewBox : System.Web.UI.UserControl
{
    Usas.Common.Cms.CurrentInfo cinfo = new Usas.Common.Cms.CurrentInfo();
    Usas.Common.Cms.Role ro = new Usas.Common.Cms.Role();
    Usas.Model.Cms.cmsReview rvm = new Usas.Model.Cms.cmsReview();
    Usas.BLL.Cms.cmsReview rv = new Usas.BLL.Cms.cmsReview();

    Usas.BLL.Cms.AspPager ap = new Usas.BLL.Cms.AspPager();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// 留言框初始化
    /// </summary>
    /// <param name="id">ArticleId</param>
    /// <param name="topic">内容标题</param>
    /// <param name="check">更多列表检查</param>
    public void ChatBoxInit(int id, string topic, string check)
    {
        Fck.ToolbarSet = "Basic";

        Fck.Height = Unit.Pixel(60);
        Fck.ToolbarStartExpanded = false;
        Fck.SkinPath = "skins/silver/";



        Button1.CommandArgument = id.ToString();
        user.InnerHtml = "您好：" + cinfo.GetEmpName() + "<" + cinfo.GetEmpId() + ">，您可以在此处发表评论";

        hy.NavigateUrl = "javascirpt:void(0)";
        hy.Attributes.Add("onclick", "LightBox('评论列表','ChatList.aspx?Id=" + id + "&IsUrl=" + ro.GetMd5Str(id + "IsUrlTrue") + "&topic=" + Server.UrlEncode(topic) + "&check=" + check + "','850','650','',true);return false");

        DataTable dt = null;
        string usasdb = System.Configuration.ConfigurationManager.AppSettings["ConnectionStringUsas"].ToString();
 
        string where = " (Ischeck='0' or Ischeck is null) and ArtId=" + id.ToString();
        string table = " cmsReview as table1 inner join opendatasource('SQLOLEDB','" + usasdb + "').Usasdb.dbo.Employees as table2 on(table1.Reviewer=table2.employeeid)";
        string PK = "ReviewId";
        string cols = "ReviewId,ArtId,Reviewer,Content,EditTime,IsCheck,table2.EmpName";


        dt = ap.ShowListDesc(table, where, PK, 5, 1, cols);

        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                RptChatList.DataSource = dt;
                RptChatList.DataBind();
            }
            else
            {
                SmallChat.Visible = false;
                hy.Attributes.Remove("onclick");
                hy.Text = "没有评论";
                hy.NavigateUrl = "";
            }
        }
        else
        {
            SmallChat.Visible = false;
            hy.Attributes.Remove("onclick");
            hy.Text = "没有评论";
            hy.NavigateUrl = "";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        rvm.ArtId = Convert.ToInt32(Button1.CommandArgument);
        rvm.EditTime = DateTime.Now;
        rvm.IsCheck = false;
        rvm.Reviewer = cinfo.GetEmpId();
        rvm.Content = Fck.Value;
        if (rvm.Content == "")
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('评论内容不得为空！');", true);
            return;
        }
        if (rv.Add(rvm) > 0)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('评论成功！');location.href='" + Request.Url.ToString() + "'", true);
        }
    }

    protected void RptChatList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            LinkButton ldel = (LinkButton)e.Item.FindControl("Del");
            ldel.CommandName = "Del";
            ldel.CommandArgument = drv["ReviewId"].ToString();
            if (cinfo.GetSuperAdminGroup())
            {
                ldel.Visible = true;
            }
            else if (ro.GetMd5Str(Request["id"].ToString() + "checkTrue") == Request["check"].ToString())
            {
                ldel.Visible = true;
            }
            else
            {
                if (drv["Reviewer"].ToString() == cinfo.GetEmpId())
                {
                    ldel.Visible = true;
                }
                else
                {
                    ldel.Visible = false;
                }
            }
        }
    }


    protected void RptChatList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            rv.Delete(Convert.ToInt32(e.CommandArgument));
            Page.ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('执行成功！');location.href='" + Request.Url.ToString() + "'", true);

        }
    }
}
